

namespace SharePointExporter.FileSystem.CommandLine
{

    using System;
    using System.IO;
    using SharePoint.ImportExport.Core;
    using SharePointExporter.FileSystem;
    /// <summary>
	/// This is the main class for the file exporter.
	/// </summary>
	class FSExporter
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		static string version = "1.0";
		private static void IncomingEventLogData(object sender, SPEventArgs e)
		{
			string logArgs = DateTime.Now.ToLongTimeString()+ ":  " + e.Message;
			Console.WriteLine(logArgs);

		}
		[STAThread]
		static void Main(string[] args)
		{
			FileParams p = new FileParams();
			if(!p.ProcessArgs(args))
			{
				Console.WriteLine("cmdFileShareExporter Version {0}", version);
				Console.WriteLine("Incorrect parameters!");
				Console.WriteLine("");
				Console.WriteLine("---");
				Console.WriteLine("Usage:");
				Console.WriteLine("-folder x:\\folder");
				Console.WriteLine("-out x:\\outputfolder");
				Console.WriteLine("-managedpath [Site/Area]");
				Console.WriteLine("-name sitename");	
				Console.WriteLine("");
				Console.WriteLine("Optional:");
				Console.WriteLine("-mode [single|multi|subsite]");
				Console.WriteLine("-library (Name of DocumentLibrary if in single mode)");
				Console.WriteLine("-managedpath [Sites|Topics]");
                Console.WriteLine("-listtemplate (Name of the list template to use)");
                Console.WriteLine("-sitedefinition (Name of the site definition to use, STS#0,STS#1 etc)");
                Console.WriteLine("-exclusions .exe;.com");
				Console.WriteLine("-inclusions .doc;.xls");
				Console.WriteLine("-datethreshold dd.mm.yyyy (in local format)");
				Console.WriteLine("-owner DOMAIN\\User");
				Console.WriteLine("-contact user@domain.com");
				Console.WriteLine("-reportLevel Information|Warning|Error");
				Console.WriteLine("");
				Console.WriteLine("Press any key to continue...");
				Console.ReadLine();
				return;

			}
			DirectoryInfo di = new DirectoryInfo(p.StartDirectory);
			if(p.DocumentLibraryName.Length<1)
				p.DocumentLibraryName=di.Name;
			if(!di.Exists)
			{
				Console.WriteLine("Start directory {0} does not exist",p.StartDirectory);
				Console.WriteLine("Press enter to continue...");
				Console.ReadLine();
				Logger.Log(ErrorLevel.Error,"Start directory missing, exiting!");
				return;
			}
            p.OutputDirectory = p.StartDirectory;

			Logger.Init(p.OutputDirectory.TrimEnd("\\".ToCharArray()) + "\\FSOut.log",p.ReportLevel);
			Logger.LogDataSendingEvent += new Logger.LogDataEventHandler(IncomingEventLogData);
			Logger.Log(ErrorLevel.Progress, "FSOut Version "+version+" starting...");
			FileExporter exporter = new FileExporter();
			if(!exporter.Process(p))
				Logger.Log(ErrorLevel.Error,"Error when extracting the data");
            if (!exporter.CreateContentTypes(p))
                Logger.Log(ErrorLevel.Error, "List template creation failed");
            Logger.LogDataSendingEvent -= new Logger.LogDataEventHandler(IncomingEventLogData);
			Logger.Close();
		}
	}



}
